package com.ia2011.project1.domain;

import java.util.ArrayList;

import com.ia2011.project1.debug.Debug;
import com.ia2011.project1.math.Utils;

/**
 * Personal transport of a user
 * 
 * @author andrewk
 * 
 */
public class Transport extends Debug {

	private ArrayList<User> passengers;
	private ArrayList<Spot> plan;
	
	public Transport() {
		passengers = new ArrayList<User>();
		plan = new ArrayList<Spot>();
	}
	
	public Transport(ArrayList<User> passengers, ArrayList<Spot> plan) {
		this.setPassengers(passengers);
		this.setPlan(plan);
	}

	/**
	 * Copy constructor
	 * 
	 * @param user original user to copy
	 */
	public Transport(Transport car) {
		this.passengers = new ArrayList<User>();
		for (User user : car.getPassengers()) {
			passengers.add(new User(user));
		}
		this.plan = new ArrayList<Spot>();
		for (Spot spot : car.getPlan()) {
			plan.add(new Spot(spot));
		}
	}

	@Override
	public void info() {
		info("Transport object -> \n" +
			"Seats (used): " + passengers.size() + "\n" +
			"Seats maximum (+1 driver): " + Constants.MAXIMUM_PEOPLE);
	}
	
	/**
	 * Add's passenger with a check if could be added
	 * @param passenger
	 * @return
	 */
	public boolean addPassenger(User passenger) {
		if (!isHaveFreeSeat()) return false;
		this.passengers.add(passenger);
		return true;
	}
	
	/**
	 * Check that it have free seat to add a passenger
	 * @return
	 */
	public boolean isHaveFreeSeat() {	
		if (this.getPassengers().size() + 1 > Constants.MAXIMUM_PEOPLE) {
			return false;
		}
		return true;
	}
	
	public Double getPlanTime() {
		Integer wholeDistance = 0;
		for (int i=0; i < plan.size() - 1; i++) {
			Integer distance = Utils.getDistance(plan.get(i).getCoord(), plan.get(i+1).getCoord());
			wholeDistance += distance;
		}
		Double timeSpent = Utils.getTime(wholeDistance);
		return timeSpent;
	}

	public void setPlan(ArrayList<Spot> plan) {
		this.plan = plan;
	}

	public ArrayList<Spot> getPlan() {
		return plan;
	}

	public void setPassengers(ArrayList<User> passengers) {
		this.passengers = passengers;
	}

	public ArrayList<User> getPassengers() {
		return passengers;
	}

}